
var points = [];
// defined in the PersonalPlan.jsp
//var map;
//var geocoder;
//var highlighted_marker = null;
//var point_markers = [];
var polyline;

function initializeMap() {
    if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));

        var center = new GLatLng(48.85884228699207,2.341461181640625);
        map.setCenter(center, 13);

        //clickMark2Pixel(map);
        manageControl(map);

	geocoder = new GClientGeocoder();
    }
}
//gecoder show address
function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}

function manageControl(map){
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
}

function addMarker(map,eventInfo,position){
    // Create our "tiny" marker icon
    var userIcon = new GIcon(G_DEFAULT_ICON);
    var imageIcon = getImgFromDataByType(eventInfo.event.type);
    userIcon.image = imageIcon;
    // Set up our GMarkerOptions object
    markerOptions = {
        icon:userIcon,
        draggable: true
    };

    var dragableMarker = new GMarker(position, markerOptions);

    GEvent.addListener(dragableMarker , "dragstart", function() {
        map.closeInfoWindow();
    });

    GEvent.addListener(dragableMarker,"drag", function() {
        createEncodings();
    });

    GEvent.addListener(dragableMarker , "dragend", function() {
        //var infoHtml = '<b>'+eventInfo.event.eventname+'</b><br/>'+'position:'+ '<br/>'+ dragableMarker.getLatLng().y + '<br/>' + dragableMarker.getLatLng().x;
        //dragableMarker.openInfoWindowHtml(infoHtml);
        eventInfo.event.pointX = dragableMarker.getLatLng().y;
        eventInfo.event.pointY = dragableMarker.getLatLng().x;
        eventInfo.operation = "modify";
    });

    GEvent.addListener(dragableMarker,"click", function() {
        //map.openInfoWindow(dragableMarker.getLatLng(), "clickedMsg");
        highlighted_marker = dragableMarker;
        var latlng = dragableMarker.getLatLng();
        var point = map.fromLatLngToDivPixel(latlng);
        var left = parseInt(document.getElementById("map_canvas").style.left);
        var top = parseInt(document.getElementById("map_canvas").style.top);
        var x = point.x + left;
        var y = point.y + top;
//        var x = 123;
//        var y = 123;
        showEventForm(x, y, eventInfo);
    });
    point_markers.push(dragableMarker);
    createEncodings();
    map.addOverlay(dragableMarker);
}

//Delete a mark
function deleteMark(){
    if(highlighted_marker==null){
        alert('not select a point');
        return;
    }
    if (point_markers.length > 0) {
        var point_index = findMarkerIndex(highlighted_marker);

        if (point_index >= 0 && point_index < point_markers.length) {

            if (highlighted_marker == point_markers[point_index]) {
                highlighted_marker = null;
            }

            map.removeOverlay(point_markers[point_index]);
            point_markers.splice(point_index, 1);
            createEncodings();
        }

        if (point_markers.length > 0) {
            if (point_markers == 0) {
                point_markers++;
            }
            highlighted_marker = point_markers[point_index-1];
        }
    }
}

// Returns the index of the marker in the polyline.
function findMarkerIndex(point_marker) {
    var index = -1;
    for (var  i = 0; i < point_markers.length; ++i) {
        if (point_markers[i] == point_marker) {
            index = i;
            break;
        }
    }
    return index;
}

// Create the encoded polyline and level strings
function createEncodings() {
    var i = 0;

    var plat = 0;
    var plng = 0;

    var encoded_points = "";
    var encoded_levels = "BB";

    for(i = 0; i < point_markers.length; i++) {
        var point = point_markers[i].getLatLng();
        var lat = point.lat();
        var lng = point.lng();

        var late5 = Math.round(lat * 1e5);
        var lnge5 = Math.round(lng * 1e5);

        dlat = late5 - plat;
        dlng = lnge5 - plng;

        plat = late5;
        plng = lnge5;

        encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
    }

    if (polyline) {
        map.removeOverlay(polyline);
    }

    if (point_markers.length > 1) {
        polyline = GPolyline.fromEncoded({
            color: "#0000FF",
            weight: 10,
            points: encoded_points,
            zoomFactor: 32,
            levels: encoded_levels,
            numLevels: 4
        });

        map.addOverlay(polyline);
    }
}

// Encode a signed number in the encode format.
function encodeSignedNumber(num) {
    var sgn_num = num << 1;

    if (num < 0) {
        sgn_num = ~(sgn_num);
    }

    return(encodeNumber(sgn_num));
}

// Encode an unsigned number in the encode format.
function encodeNumber(num) {
    var encodeString = "";

    while (num >= 0x20) {
        encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
        num >>= 5;
    }

    encodeString += (String.fromCharCode(num + 63));
    return encodeString;
}